package com.example.shoppingmallclient.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.shoppingmallclient.R;
import com.example.shoppingmallclient.ServerIp;
import com.example.shoppingmallclient.adapter.OrderListAdapter;
import com.example.shoppingmallclient.pojo.OrderItem;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


/**
 * @author 袁梦达 2019012364
 */
public class ShowOrderList extends AppCompatActivity {

    private List<OrderItem> list;

    private ListView listView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order_list);

        list = new ArrayList<>();
        listView = findViewById(R.id.lv);
        new getOrderList().execute();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(ShowOrderList.this, ShowOrderItem.class);
                intent.putExtra("orderId", list.get(position).getOrderId());
                startActivity(intent);
            }
        });

    }

    private class getOrderList extends AsyncTask {

        @Override
        protected Object doInBackground(Object[] objects) {
            try {
                String serverUrl = "http://" + ServerIp.ServerIp + ":8080/OrderShowServlet";
                URL url = new URL(serverUrl);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("POST");

                InputStream is = connection.getInputStream();
                StringBuffer stringBuffer = new StringBuffer();
                int len = 0;
                byte[] bytes = new byte[1024];
                while ((len = is.read(bytes)) != -1){
                    stringBuffer.append(new String(bytes, 0, len));
                }

                if(stringBuffer.toString().length() == 0){
                    return null;
                }
                String substring = null;
                if(stringBuffer.toString().length() > 1){
                    substring = stringBuffer.toString().substring(0, stringBuffer.toString().length() - 1);
                }
                String[] strings = substring.split("#");

                for (String string : strings) {
                    OrderItem orderItem = new OrderItem();
                    String[] split = string.split("@");
                    orderItem.setOrderId(Integer.valueOf(split[0]));
                    orderItem.setOrderTime(split[1]);
                    orderItem.setQuantity(Integer.valueOf(split[2]));
                    orderItem.setSumPrice(Double.parseDouble(split[3]));
                    list.add(orderItem);
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            OrderListAdapter orderListAdapter = new OrderListAdapter(list, R.layout.order_item, ShowOrderList.this);
            listView.setAdapter(orderListAdapter);
        }
    }
}
